#include "moduleaccueil.h"

ModuleAccueil::ModuleAccueil(QObject *parent) :
    QObject(parent)
{

}

QStringList ModuleAccueil::modules(){
    QStringList mod;
    mod << "Derniers_Bucques";
    mod << "Top_Sommes_Depenses";
    return mod;
}

QString ModuleAccueil::toHtml(QString module){

    if(module == "Derniers_Bucques"){
        QList<int> *pgs = new QList<int>();
        QList<QStringList *> *Consos = new QList<QStringList *>();
        QString html;

        int resultats = Statistiques::dernieresConsos(pgs,Consos);

        for(int i =0; i<resultats; i++){
            Pg pg(pgs->at(i),true);
            html += createSpan(pg.getBucque()+ " " + QString::number(pg.getFams()) + " " + pg.getProms(),QColor(Qt::red),12,ModuleAccueil::BOLD) + "<br />";
            QStringList *l = Consos->at(i);
            for(int j=0; j< l->size(); j++){
                html += createSpan(l->at(j),QColor(Qt::blue),10);
                html += "<br />";
            }
            delete l;
            html += "<br />";
        }
        delete Consos;
        delete pgs;

        return html;
    }

    if(module == "Top_Sommes_Depenses"){
        QList<int> *pgs = new QList<int>();
        QList<double> *somme = new QList<double>();
        QString html = createSpan("Top 3 des sommes dépensées :",QColor(Qt::yellow),14,BOLD) + "<br /><br />";

        int resultats = Statistiques::grosConsommateurs(pgs,somme);
        for(int i=0; i<resultats; i++){
            Pg pg(pgs->at(i),true);
            html += createSpan(pg.getBucque()+ " " + QString::number(pg.getFams()) + " " + pg.getProms()+ " : ",QColor(Qt::red),12,BOLD);
            html += createSpan(QString::number(somme->at(i)) + " €",QColor(Qt::blue),13,BOLD) + "<br />";

        }

        html += "<br />";

        delete pgs;
        delete somme;

        return html;
    }

    return "";

}

QString ModuleAccueil::createSpan(QString text, QColor couleur, int size, Attributs attr){
    QString s = "<span style=\" font-size:"+QString::number(size)+"pt; ";
    if(attr.testFlag(BOLD)){
        s += "font-weight:600; ";
    }
    if(attr.testFlag(ITALIC)){
        s += "font-style:italic; ";
    }
    if(attr.testFlag(UNDERLIGNE)){
        s += "text-decoration: underline; ";
    }

    QString c = QString::number(couleur.rgb(),16);
    c.remove(0,2);

    s += "color:#"+c+";\">";
    s += text;
    s += "</span>";

    return s;
}

